﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Category 的摘要说明
/// </summary>
public class Category
{
    public const string CategoryStr = "category";
    public int Guid;
    public int DoctorNum;
    public int StudentNum;
    public int ExpertNum;
    public int PaperNum;
    public string Name;
    public string Intro;
	public Category(int g)
	{
        this.Guid = g;
        string sql = "select * from Category where Guid=" + g.ToString();
        DataRow dr = Fun.CreateDr(sql);
        this.Name = dr["Category"].ToString().Trim();
        this.Intro = dr["Intro"].ToString().Trim();
        sql = "select count(*) as n from Doctor where CategoryGuid=" + g.ToString();
        this.DoctorNum = Convert.ToInt32(Fun.GetField(sql));
        sql = "select count(*) as n from Doctor where Expert=1 and CategoryGuid=" + g.ToString();
        this.ExpertNum = Convert.ToInt32(Fun.GetField(sql));
        sql = "select count(*) as n from Student where DoctorGuid in (select Guid from Doctor where CategoryGuid=" + g.ToString() + ")";
        this.StudentNum = Convert.ToInt32(Fun.GetField(sql));
        sql = "select count(*) as n from Relationship where Relationship='" + Paper.StrPaper + "' and GuidOne in (select Guid from Doctor where CategoryGuid=" + g.ToString() + ")";
        this.PaperNum = Convert.ToInt32(Fun.GetField(sql));
    }

    public DataTable Doctors()
    {
        string sql = "select UserGuid as g from Doctor where CategoryGuid="+this.Guid.ToString()+" order by UserGuid asc";
        return Fun.CreateDt(sql);
    }

    public static int Count()
    {
        string sql = "select count(*) as n from Category";
        return Convert.ToInt32(Fun.GetField(sql, "n"));
    }

    public void Update()
    {
        string sql = "update Category set Category='" + Fun.L(this.Name) + "',Intro='" + Fun.L(this.Intro) + "' where Guid=" + this.Guid.ToString();
        Fun.Execute(sql);
    }

    public static int Add(string n, string i,ref string tip)
    {
        if(n==""||i=="")
        {
            tip="name和intro不能为空";
            return 0;
        }
        string sql = "select count(*) as n from Category where Category='"+Fun.L(n)+"'";
        if (Convert.ToInt32(Fun.GetField(sql)) > 0)
        {
            tip = "该类型名称已经存在";
            return 0;
        }
        int g = Fun.Guid();
        sql = "insert into Category(Guid,Category,Intro) values(" + g.ToString() + ",'" + Fun.L(n) + "','" + Fun.L(i) + "')";
        Fun.Execute(sql);
        return g; ;
    }

    public static DataTable CategorysDt()
    {
        string sql = "select Guid from Category order by Guid asc";
        return Fun.CreateDt(sql);
    }

    public static DataTable Categorys(bool hasAll)
    {

        DataTable dt = new DataTable();
        dt.Columns.Add("value", Params.IntType);
        dt.Columns.Add("text", Params.StrType);
        DataRow dr;
        if (hasAll)
        {
            dr = dt.NewRow();
            dr["text"] = "所有类型";
            dr["value"] = -1;
            dt.Rows.Add(dr);
        }
        dr = dt.NewRow();
        dr["text"] = "无";
        dr["value"] = 0;
        dt.Rows.Add(dr);
        string sql = "select Guid,Intro from Category order by Guid asc";
        DataTable dt2 = Fun.CreateDt(sql);
        foreach (DataRow dr2 in dt2.Rows)
        {
            dr = dt.NewRow();
            dr["text"] = dr2["Intro"].ToString().Trim();
            dr["value"] = Convert.ToInt32(dr2["Guid"]);
            dt.Rows.Add(dr);
        }
        return dt;
    }

    public static void Delete(int g)
    {
        string sql = "update Doctor set CategoryGuid=0 where CategoryGuid="+g.ToString();
        Fun.Execute(sql);
        sql = "update Student set CategoryGuid=0 where CategoryGuid=" + g.ToString();
        Fun.Execute(sql);
        sql = "delete from Category where Guid="+g.ToString();
        Fun.Execute(sql);
    }

    public static string CategoryName(int g)
    {
        string sql = "select Category from Category where Guid="+g.ToString();
        DataTable dt = Fun.CreateDt(sql);
        if (dt.Rows.Count < 1)
        {
            return "无";
        }
        return dt.Rows[0][0].ToString().Trim();
    }
}
